package com.example.wan1.view.collect;

import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.wan1.R;
import com.example.wan1.adapter.CollectWebAdapter;
import com.example.wan1.adapter.MixedAdapter;
import com.example.wan1.adapter.ShareAdapter;
import com.example.wan1.base.fragment.SimpleFragment;
import com.example.wan1.bus.RxBus;
import com.example.wan1.event.NotifyOperateEvent;
import com.example.wan1.model.ArticleBean;
import com.example.wan1.model.CoinBean;
import com.example.wan1.model.CollectArticleBean;
import com.example.wan1.model.CollectWebBean;
import com.example.wan1.model.MixedEntity;
import com.example.wan1.model.common.CommonResList;
import com.example.wan1.model.common.CommonResMultiList;
import com.example.wan1.utils.ToastUtils;
import com.example.wan1.utils.ToolsUtils;
import com.example.wan1.view.web.JumpTool;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.scwang.smart.refresh.header.MaterialHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import butterknife.BindView;

/***
 * 创建时间：2020/7/26 9:36
 * 创建人：10850
 * 功能描述：我的收藏--功能
 * 用户中心也需要使用PerCollectFragment，不过需要控制下拉刷新这个是否生效，所以增加一个变量，来控制
 */
public class PerCollectFragment extends SimpleFragment implements OnLoadMoreListener, OnRefreshListener
        , CollectContract.View {
    private static String PARAM = "PARAM";

    @BindView(R.id.srlheader)
    MaterialHeader mSrlheader;
    @BindView(R.id.collect_rv)
    RecyclerView mCollectRv;
    @BindView(R.id.srl)
    SmartRefreshLayout mSrl;
    @BindView(R.id.fab_add)
    FloatingActionButton mFabAdd;

    private String type;
    private boolean isNoRefresh;
    private int id;

    @Inject
    CollectPresenter mPresenter;

    private MixedAdapter mAdapter;
    private List<MixedEntity> mMixedList;
    private CollectWebAdapter mWebAdapter;
    private List<CollectWebBean> mWebList;
    private ShareAdapter mShareAdapter;
    private List<ArticleBean> mShareList;

    private int curPage = 0;//当前页
    private int sharePage = 1;
    private ProgressDialog mProgressDialog;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Bundle arguments = getArguments();
        type = arguments.getString(PARAM);
        isNoRefresh = arguments.getBoolean("isNoRefresh");
        id = arguments.getInt("id", -1);
    }

    @Override
    protected void initData() {
        if ("收藏文章".equals(type)) {
            mPresenter.getCollectArticleList(curPage);
        } else if ("收藏网站".equals(type)) {
            mPresenter.getCollectWebList();
        } else if ("我的分享".equals(type)) {
            if (id == -1)
                mPresenter.getShareArticleList(curPage);
            else
                mPresenter.getShareList(id, curPage);
        }
    }

    @Override
    protected void initView() {
        showLoading();
        //mSrlheader.setColorSchemeResources(R.color.qc_green);

        mSrl.setOnLoadMoreListener(this);
        mSrl.setOnRefreshListener(this);

        mSrl.setEnableRefresh(false);
        mSrl.setEnableLoadMore(false);
        //空数据的布局
        TypedValue typedValue = new TypedValue();
        mContext.getTheme().resolveAttribute(R.attr.colorPrimary,
                typedValue, true);
        View footer = LayoutInflater.from(mContext).inflate(R.layout.empty_item, mSrl, false);
        ImageView iv = footer.findViewById(R.id.empty_iv);
        iv.setColorFilter(typedValue.data);

        if ("收藏文章".equals(type)) {
            mMixedList = new ArrayList<>();
            mAdapter = new MixedAdapter(mMixedList);

            mAdapter.bindToRecyclerView(mCollectRv);
            mCollectRv.setAdapter(mAdapter);

            //点击事件的处理
            mAdapter.setOnItemClickListener((baseQuickAdapter, view, i) -> JumpTool.goWeb(mContext, mMixedList.get(i).getBean().getTitle()
                    , mMixedList.get(i).getBean().getLink()));

            mAdapter.setEmptyView(footer);

            mAdapter.setOnItemChildClickListener((baseQuickAdapter, view, i) -> {
                switch (view.getId()) {
                    case R.id.article_iv_collect:
                    case R.id.project_list_collect:
                        //如果是收藏的话，那么需要发送事件的
                        HandleCollectTool.getInstance().cancelMyCollect(mMixedList.get(i).getBean().getId(),
                                mMixedList.get(i).getBean().getOriginId());
                        showProgressDialog();
                        isCancelArticle = true;
                        break;
                }
            });

        } else if ("收藏网站".equals(type)) {
            mWebList = new ArrayList<>();
            mWebAdapter = new CollectWebAdapter(R.layout.collect_web_item, mWebList);
            mWebAdapter.bindToRecyclerView(mCollectRv);
            mCollectRv.setAdapter(mWebAdapter);

            //点击事件的处理
            mWebAdapter.setOnItemClickListener((baseQuickAdapter, view, i) -> JumpTool.goWeb(mContext, mWebList.get(i).getName()
                    , mWebList.get(i).getLink()));

            mWebAdapter.setEmptyView(footer);

            mWebAdapter.setOnItemLongClickListener((adapter, view, position) -> {
                //执行请求，删除收藏的网站
                delPop(position);
                return true;
            });

            mWebAdapter.setOnItemChildClickListener((adapter, view, position) -> {
                //弹出一个dialog，修改内容并且保存
                editDialog(position);
            });
        } else if ("我的分享".equals(type)) {
            mShareList = new ArrayList<>();
            mShareAdapter = new ShareAdapter(R.layout.share_item, mShareList);
            mShareAdapter.bindToRecyclerView(mCollectRv);
            mCollectRv.setAdapter(mShareAdapter);
            mShareAdapter.setEmptyView(footer);

            mShareAdapter.setOnItemChildClickListener((baseQuickAdapter, view, i) -> delShareDialog(i));
            mShareAdapter.setOnItemClickListener((baseQuickAdapter, view, i) ->
                    JumpTool.goWeb(mContext, mShareAdapter.getData().get(i).getTitle()
                            , mShareAdapter.getData().get(i).getLink()));
        }
        mCollectRv.setLayoutManager(new LinearLayoutManager(getActivity()));
        if (id == -1) mFabAdd.show();
        else mFabAdd.hide();
        mFabAdd.setOnClickListener(v -> {
            if ("收藏网站".equals(type)) {
                addDialog();
            } else if ("我的分享".equals(type)) {
                addShareDialog();
            } else if ("收藏文章".equals(type)) {
                addArticleDialog();
            }
        });

        int color = ToolsUtils.getThemeColor(getActivity());
        if (color != 0) {
            //mFabAdd.setRippleColor(color);//点击效果
            //mFabAdd.setColorFilter(color);//改的是加号的颜色
            mFabAdd.setBackgroundTintList(ColorStateList.valueOf(color));
            iv.setColorFilter(color);
        }

        RxBus.getDefault().subscribe(this, new RxBus.Callback<String>() {
            @Override
            public void onEvent(String s) {
                if (s.equals("refreshAll")) {
                    mCollectRv.smoothScrollToPosition(0);
                    onRefresh(mSrl);
                }
            }
        });
    }

    @Override
    protected int initLayoutRes() {
        return R.layout.fragment_per_collect;
    }

    public PerCollectFragment newInstance(String type) {
        PerCollectFragment baseFragment = new PerCollectFragment();
        Bundle bundle = new Bundle();
        bundle.putString(PARAM, type);
        baseFragment.setArguments(bundle);
        return baseFragment;
    }

    public PerCollectFragment newInstance(String type, boolean isNoRefresh) {
        PerCollectFragment baseFragment = new PerCollectFragment();
        Bundle bundle = new Bundle();
        bundle.putString(PARAM, type);
        bundle.putBoolean("isNoRefresh", isNoRefresh);
        baseFragment.setArguments(bundle);
        return baseFragment;
    }

    public PerCollectFragment newInstance(String type, boolean isNoRefresh, int id) {
        PerCollectFragment baseFragment = new PerCollectFragment();
        Bundle bundle = new Bundle();
        bundle.putString(PARAM, type);
        bundle.putBoolean("isNoRefresh", isNoRefresh);
        bundle.putInt("id", id);
        baseFragment.setArguments(bundle);
        return baseFragment;
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        mCollectRv.postDelayed(() -> {
            if ("收藏文章".equals(type)) {
                mPresenter.getCollectArticleList(curPage);
            } else if ("收藏网站".equals(type)) {
                mPresenter.getCollectWebList();
            } else if ("我的分享".equals(type)) {
                if (id == -1)
                    mPresenter.getShareArticleList(sharePage);
                else
                    mPresenter.getShareList(id, sharePage);
            }
            mSrl.finishLoadMore(true);
        }, 200);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        mCollectRv.postDelayed(() -> {
            if ("收藏文章".equals(type)) {
                mMixedList.clear();
                mPresenter.getCollectArticleList(0);
            } else if ("收藏网站".equals(type)) {
                mWebList.clear();
                mPresenter.getCollectWebList();
            } else if ("我的分享".equals(type)) {
                mShareList.clear();
                if (id == -1)
                    mPresenter.getShareArticleList(1);
                else
                    mPresenter.getShareList(id, 1);
            }
            mSrl.finishRefresh(true);
        }, 200);
    }

    @Override
    public void onArticleSuccess(CommonResList<CollectArticleBean> data) {
        //这里可以改成一种类型
        hideLoading();
        if (data.getDatas().size() > 0) {
            if (!isNoRefresh)
                mSrl.setEnableRefresh(true);
            mSrl.setEnableLoadMore(true);
        }
        if (data.isOver() && data.getDatas().size() < 20) {
            mAdapter.loadMoreEnd();
            if (mAdapter.getFooterLayoutCount() == 0 && data.getDatas().size() != 0) {
                View footer = LayoutInflater.from(mContext).inflate(R.layout.loadmore_view, null);
                mAdapter.addFooterView(footer);
            }
            mSrl.setEnableLoadMore(false);
        }
        if (data.getDatas().size() == 0) {
            mAdapter.notifyDataSetChanged();
            return;
        }

        List<CollectArticleBean> datas = data.getDatas();
        for (int i = 0; i < datas.size(); i++) {
            MixedEntity item;
            if (TextUtils.isEmpty(datas.get(i).getEnvelopePic()))
                item = new MixedEntity(MixedEntity.ART, datas.get(i));
            else
                item = new MixedEntity(MixedEntity.PRO, datas.get(i));
            mMixedList.add(item);
        }
        curPage = data.getCurPage();
        if (mAdapter != null) {//todo..这里的适配器有可能是空的，不知道为什么
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onWebSuccess(List<CollectWebBean> data) {
        hideLoading();
        if (data.size() > 0) {
            if (!isNoRefresh)
                mSrl.setEnableRefresh(true);
            mSrl.setEnableLoadMore(true);
        }
        if (data.size() < 20) {
            mWebAdapter.loadMoreEnd();
            if (mWebAdapter.getFooterLayoutCount() == 0 && data.size() != 0) {
                View footer = LayoutInflater.from(mContext).inflate(R.layout.loadmore_view, null);
                mWebAdapter.addFooterView(footer);
            }
            mSrl.setEnableLoadMore(false);
        }
        if (data.size() != 0)
            mWebAdapter.setNewData(data);
            //mWebAdapter.addData(data);
        else {
            //设置空的view
        }

        mWebAdapter.notifyDataSetChanged();
    }

    @Override
    public void onShareSuccess(CommonResMultiList<CoinBean, CommonResList<ArticleBean>> data) {
        hideLoading();
        if (data.getData2().getDatas().size() > 0) {
            if (!isNoRefresh)
                mSrl.setEnableRefresh(true);
            mSrl.setEnableLoadMore(true);
        }
        if (data.getData2().isOver() && data.getData2().getDatas().size() < 20) {
            mShareAdapter.loadMoreEnd();
            if (mShareAdapter.getFooterLayoutCount() == 0 && data.getData2().getDatas().size() != 0) {
                View footer = LayoutInflater.from(mContext).inflate(R.layout.loadmore_view, null);
                mShareAdapter.addFooterView(footer);
            }
            mSrl.setEnableLoadMore(false);
        }
        if (data.getData2().getDatas().size() != 0) {
            if (data.getData2().getCurPage() == 1) {
                mShareAdapter.setNewData(data.getData2().getDatas());
            } else
                mShareAdapter.addData(data.getData2().getDatas());
            sharePage = data.getData2().getCurPage() + 1;
            //加了一个处理，考虑到不停的对列表数据做增删改的话，list数据容易不对。所以加了这么个处理
            if (mShareList != mShareAdapter.getData()) {
                mShareList = mShareAdapter.getData();
            }
        }
        mShareAdapter.notifyDataSetChanged();

    }

    @Override
    public void onFailure(String msg) {
        ToastUtils.showToast(msg);
    }

    private TextView mEditTvTitle;
    private EditText mEditEtName;
    private EditText mEditEtLink;
    private Button mEditBtn1;
    private Button mEditBtn2;
    private ImageView mEditIvClose;

    public void editDialog(final int pos) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.edit_item, null, false);
        final AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity(), R.style.DialogTheme)
                .setView(view)
                .setCancelable(true);
        final AlertDialog show = dialog.show();

        mEditEtName = view.findViewById(R.id.edit_et_name);
        mEditEtLink = view.findViewById(R.id.edit_et_link);
        mEditBtn1 = view.findViewById(R.id.edit_btn1);
        mEditBtn2 = view.findViewById(R.id.edit_btn2);
        mEditIvClose = view.findViewById(R.id.edit_iv_close);
        mEditTvTitle = view.findViewById(R.id.edit_tv_title);

        mEditEtName.setText(mWebList.get(pos).getName());
        mEditEtLink.setText(mWebList.get(pos).getLink());
        mEditBtn1.setOnClickListener(v -> show.dismiss());

        mEditBtn2.setOnClickListener(v -> {
            //2020/3/11 执行更新操作,然后需要刷新列表(清除之前的数据，重新获取)
            HandleCollectTool.getInstance().editCollect(mWebList.get(pos).getId(),
                    mEditEtName.getText().toString(), mEditEtLink.getText().toString());
            showProgressDialog();
            show.dismiss();
        });

        mEditIvClose.setOnClickListener(v -> show.dismiss());
    }

    public void delPop(final int pos) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.del_web_item, null, false);
        final PopupWindow popupWindow = new PopupWindow(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        popupWindow.setContentView(view);
        //是否需要点击PopupWindow外部其他界面时候消失
//        popupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
//        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.pop_window_bg));
        popupWindow.setOutsideTouchable(true);

        popupWindow.setTouchable(true);
        popupWindow.setFocusable(true);

        //重新设置大小
        popupWindow.setHeight(60);
        int width = getActivity().getWindowManager().getDefaultDisplay().getWidth();
        popupWindow.setWidth(width * 3 / 4);


        popupWindow.showAtLocation(mCollectRv, Gravity.CENTER, 0, 0);

        dimBackground(1f, 0.6f);

        TextView tv = view.findViewById(R.id.del_web_tv);
        tv.setOnClickListener(v -> {
            // TODO: 2020/3/11 增加一个popupwindow，用来处理删除逻辑
            HandleCollectTool.getInstance().delCollect(mWebList.get(pos).getId());
            showProgressDialog();
            popupWindow.dismiss();
        });

        popupWindow.setOnDismissListener(() -> bgAlpha(1f));
    }

    /**
     * 设置窗口的背景透明度
     *
     * @param f 0.0-1.0
     */
    private void bgAlpha(float f) {
        WindowManager.LayoutParams layoutParams = getActivity().getWindow().getAttributes();
        layoutParams.alpha = f;
        getActivity().getWindow().setAttributes(layoutParams);
    }

    private void dimBackground(final float from, final float to) {
        final Window window = getActivity().getWindow();
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(from, to);
        valueAnimator.setDuration(500);
        valueAnimator.addUpdateListener(animation -> {
            WindowManager.LayoutParams params = window.getAttributes();
            params.alpha = (Float) animation.getAnimatedValue();
            getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            window.setAttributes(params);
        });
        valueAnimator.start();
    }

    public void addDialog() {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.edit_item, null, false);
        final AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity(), R.style.DialogTheme)
                .setView(view)
                .setCancelable(true);
        final AlertDialog show = dialog.show();

        mEditEtName = view.findViewById(R.id.edit_et_name);
        mEditEtLink = view.findViewById(R.id.edit_et_link);
        mEditBtn1 = view.findViewById(R.id.edit_btn1);
        mEditBtn2 = view.findViewById(R.id.edit_btn2);
        mEditIvClose = view.findViewById(R.id.edit_iv_close);
        mEditTvTitle = view.findViewById(R.id.edit_tv_title);
        mEditTvTitle.setText("添加收藏网站");

        mEditBtn1.setOnClickListener(v -> show.dismiss());

        mEditBtn2.setOnClickListener(v -> {
            //2020/3/11 执行更新操作,然后需要刷新列表(清除之前的数据，重新获取)
            HandleCollectTool.getInstance().addCollect(
                    mEditEtName.getText().toString(), mEditEtLink.getText().toString());
            showProgressDialog();
            show.dismiss();
        });

        mEditIvClose.setOnClickListener(v -> show.dismiss());
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        RxBus.getDefault().subscribe(mContext, new RxBus.Callback<NotifyOperateEvent>() {
            @Override
            public void onEvent(NotifyOperateEvent event) {
                if ("网站".equals(event.getType())) {
                    mWebList.clear();
                    mPresenter.getCollectWebList();
                    mProgressDialog.dismiss();
                } else if ("分享".equals(event.getType())) {
                    //mShareList.clear();
                    mShareAdapter.removeAllFooterView();
                    mShareAdapter.setNewData(null);
                    mPresenter.getShareArticleList(1);
                    mProgressDialog.dismiss();
                } else if ("收藏".equals(event.getType())) {
                    //todo..这里的为什么接收到两次？
                    Log.i("zxd", "onEvent: ");
                    mMixedList.clear();
                    mPresenter.getCollectArticleList(0);
                    mProgressDialog.dismiss();
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RxBus.getDefault().unregister(mContext);
    }

    public void showProgressDialog() {
        mProgressDialog = new ProgressDialog(mContext);
        //dialog.setTitle("我是标题");
        mProgressDialog.setMessage("等待中...");
        mProgressDialog.setIndeterminate(true);
        mProgressDialog.setCancelable(false);
        mProgressDialog.show();
    }

    /***
     * 删除分享的文章
     * @param pos
     */
    public void delShareDialog(int pos) {
        final AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);

        dialog.setOnCancelListener(dialog1 -> dialog1.dismiss());

        dialog.setOnDismissListener(dialog12 -> dialog12.dismiss());

        dialog  //.setIcon(R.mipmap.ic_launcher)
                //.setTitle("我是标题")
                .setMessage("确定删除<" + mShareAdapter.getData().get(pos).getTitle() + ">文章吗？")
                .setCancelable(true)
                .setPositiveButton("确定",
                        (dialog14, which) -> {
                            //删除逻辑
                            HandleCollectTool.getInstance().delShare(mShareAdapter.getData().get(pos).getId());
                            showProgressDialog();
                            dialog14.dismiss();
                        })
                .setNegativeButton("取消",
                        (dialog13, which) -> dialog13.dismiss());
        dialog.show();
    }

    public void addShareDialog() {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.edit_item, null, false);
        view.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT));
        final AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity(), R.style.DialogTheme)
                .setView(view)
                .setCancelable(true);
        final AlertDialog show = dialog.show();

        mEditEtName = view.findViewById(R.id.edit_et_name);
        mEditEtLink = view.findViewById(R.id.edit_et_link);
        mEditBtn1 = view.findViewById(R.id.edit_btn1);
        mEditBtn2 = view.findViewById(R.id.edit_btn2);
        mEditIvClose = view.findViewById(R.id.edit_iv_close);
        mEditTvTitle = view.findViewById(R.id.edit_tv_title);
        mEditTvTitle.setText("添加分享");

        mEditBtn1.setOnClickListener(v -> show.dismiss());

        mEditBtn2.setOnClickListener(v -> {
            //2020/3/11 执行更新操作,然后需要刷新列表(清除之前的数据，重新获取)
            HandleCollectTool.getInstance().addShare(
                    mEditEtName.getText().toString(), mEditEtLink.getText().toString());
            showProgressDialog();
            show.dismiss();
        });

        mEditIvClose.setOnClickListener(v -> show.dismiss());
    }

    private EditText author;

    public void addArticleDialog() {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.edit_item, null, false);
        final AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity(), R.style.DialogTheme)
                .setView(view)
                .setCancelable(true);
        final AlertDialog show = dialog.show();

        mEditEtName = view.findViewById(R.id.edit_et_name);
        mEditEtLink = view.findViewById(R.id.edit_et_link);
        mEditBtn1 = view.findViewById(R.id.edit_btn1);
        mEditBtn2 = view.findViewById(R.id.edit_btn2);
        mEditIvClose = view.findViewById(R.id.edit_iv_close);
        mEditTvTitle = view.findViewById(R.id.edit_tv_title);
        author = view.findViewById(R.id.edit_et_author);
        author.setVisibility(View.VISIBLE);
        mEditTvTitle.setText("添加站外文章收藏");

        mEditBtn1.setOnClickListener(v -> show.dismiss());

        mEditBtn2.setOnClickListener(v -> {
            //2020/3/11 执行更新操作,然后需要刷新列表(清除之前的数据，重新获取)
            HandleCollectTool.getInstance().collectOut(
                    mEditEtName.getText().toString(),
                    author.getText().toString(),
                    mEditEtLink.getText().toString());
            showProgressDialog();
            show.dismiss();
        });

        mEditIvClose.setOnClickListener(v -> show.dismiss());
    }

    private boolean isCancelArticle = false;

    @Override
    public void onPause() {
        super.onPause();
        if (isCancelArticle)
            RxBus.getDefault().post("cancelArticle");
    }
}
